<?php
namespace app\mxty\controller;

use think\facade\Request;
use app\mxty\logic\Arena as ArenaLogic;
use app\api\controller\Controller as ApiController;

class Arena extends ApiController
{
    // 获取指定场地id下的所有半场信息
    public function parts()
    {
        $params = Request::only([
            'arena_id',
            'year',
            'month',
            'date'
        ]);

        $date = "{$params['year']}-{$params['month']}-{$params['date']}";
        $result = (new ArenaLogic)->getPartsInfoByAreanId($date, $params['arena_id']);
        return success($result);
    }

    /**
     * 获取所有开启的场地列表接口
     */
    public function list()
    {
        $params = Request::only([
            'year',
            'month',
            'date'
        ]);

        $date = "{$params['year']}-{$params['month']}-{$params['date']}";

        $arenas = (new ArenaLogic)->getIdleArenaInfo($date);
        return success($arenas);
    }
	public function booktime()
    {
        return success(22);
    }

    /**
     * 查询某个时间段是否有裁判已被预约
     */
    public function refereeInfo($date, $timeFragment)
    {
        $referees = (new ArenaLogic)->getBookingReferees($date, $timeFragment);
        return $this->renderSuccess($referees);
    }

    // 会员预订场地
    public function book()
    {
        $params = Request::only([
            'year',
            'month',
            'date',
            'part_time_fragment_ids',
            'refereeIds'
        ]);

        $refereeIds = explode(',', $params['refereeIds']);
        $date = "{$params['year']}-{$params['month']}-{$params['date']}";
        $result = (new ArenaLogic)->bookArena($date, explode(',', $params['part_time_fragment_ids']), $refereeIds);

        return $this->renderSuccess($result);
    }

    // 获取订场二维码token
    public function codeToken() {
        $token = (new ArenaLogic)->generateCodeToken();
        return $this->renderSuccess(compact('token'));
    }

    // 获取订场二维码信息
    public function checkBooking($bookingFragmentId, $codeToken)
    {
        $booking = (new ArenaLogic)->checkBooking($bookingFragmentId, $codeToken);
        return $this->renderSuccess($booking);
    }
    
    // 确认用户到场接口
    public function present($bookingFragmentId, $codeToken)
    {
        (new ArenaLogic)->present($bookingFragmentId, $codeToken);
        return $this->renderSuccess('确认成功');
    }
}
